package com.damie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dadie.Domain.SiteDomain;
import com.dadie.Domain.UserDomain;
import com.dadie.Domain.WasteOrderDetailsDomain;
import com.dadie.Domain.WasteOrderDomain;
import com.dadie.Dto.UserAddressDto;
import com.dadie.Dto.UserWasteOrderDto;
import com.dadie.Dto.WasteOrderDetailsDto;
import com.dadie.Dto.WasteOrderDto;
import com.dadie.parame.WasteOrderParame;
import com.damie.facade.UserFacade;
import com.damie.mapper.SiteMapper;
import com.damie.mapper.UserMapper;
import com.damie.mapper.WasteOrderDetailsMapper;
import com.damie.mapper.WasteOrderMapper;
import com.damie.service.WasteOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.damie.uilt.Copy;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alon
 * @since 2021-01-22
 */
@Service
public class WasteOrderServiceImpl extends ServiceImpl<WasteOrderMapper, WasteOrderDomain> implements WasteOrderService {
    @Resource
    private WasteOrderMapper mapper;
    @Resource
    private WasteOrderDetailsMapper detailsMapper;
    @Resource
    private UserMapper usermapper;
    @Resource
    private SiteMapper siteMapper;
    @Resource
    private UserFacade userFacade;

    @Override
    public Page<UserWasteOrderDto> selectAllOrder(Integer index, Integer size, WasteOrderParame wasteOrderParame) {
        QueryWrapper<WasteOrderDomain> objectQueryWrapper = new QueryWrapper<>();
        if (wasteOrderParame.getUserId()!=null&&wasteOrderParame.getUserId()!=""){
            QueryWrapper<UserDomain> userDomainQueryWrapper = new QueryWrapper<>();
            userDomainQueryWrapper.like("user_password",wasteOrderParame.getUserId());
            List<UserDomain> userDomains = usermapper.selectList(userDomainQueryWrapper);
            List<String> collect = userDomains.stream().map(userDomain -> {
                String userId = userDomain.getUserId();
                return userId;
            }).collect(Collectors.toList());
            objectQueryWrapper.in("user_id",collect);
        }
        if (wasteOrderParame.getSiteId()!=null&&wasteOrderParame.getSiteId()!=""){
            QueryWrapper<SiteDomain> siteDomainQueryWrapper = new QueryWrapper<>();
            siteDomainQueryWrapper.like("site_name",wasteOrderParame.getSiteId());
            List<SiteDomain> siteDomains = siteMapper.selectList(siteDomainQueryWrapper);
            List<String> collect = siteDomains.stream().map(siteDomain -> {
                String siteId = siteDomain.getSiteId();
                return siteId;
            }).collect(Collectors.toList());
            objectQueryWrapper.in("site_id",collect);
        }
        if (wasteOrderParame.getCreateTime()!=null){
            objectQueryWrapper.ge("create_time",wasteOrderParame.getCreateTime());
        }
        if (wasteOrderParame.getWasteOrderStatus()!=null){
            switch (wasteOrderParame.getWasteOrderStatus()){
                case 1:
                    objectQueryWrapper.eq("waste_order_status",1);
                    break;
                case 2:
                    objectQueryWrapper.eq("waste_order_status",2);
                    break;
                case 3:
                    objectQueryWrapper.eq("waste_order_status",3);
                    break;
                default:
                    objectQueryWrapper.eq("waste_order_status",4);
                    break;
            }
        }
       Page<WasteOrderDomain> wasteOrderDomainPage = mapper.selectPage(new Page<WasteOrderDomain>(index, size), objectQueryWrapper);
        List<WasteOrderDto> collect = wasteOrderDomainPage.getRecords().stream().map(wasteOrderDomain -> {
            WasteOrderDto wasteOrderDto = new WasteOrderDto();
            BeanUtils.copyProperties(wasteOrderDomain, wasteOrderDto);
            return wasteOrderDto;
        }).collect(Collectors.toList());
        List<UserWasteOrderDto> collect1 = collect.stream().map(c -> {
            UserWasteOrderDto userWasteOrderDto = new UserWasteOrderDto();
            String userId = c.getUserId();
            UserDomain userDomain = usermapper.selectById(userId);
            userWasteOrderDto.setWasteOrderId(c.getWasteOrderId());
            userWasteOrderDto.setUserName(userDomain.getUserPassword());
            userWasteOrderDto.setUserPhone(c.getUserPhone());
            userWasteOrderDto.setAppointmentTime(c.getAppointmentTime());
            userWasteOrderDto.setCreateTime(c.getCreateTime());

            String s = userFacade.queryById(c.getUserAddressId()).getData().get("userAddress").toString();
            String substring = s.substring(s.indexOf("addressName="), s.lastIndexOf(", addressLongitude="));
            String replace = substring.replace("addressName=", "");

            userWasteOrderDto.setUserAddressName(replace);
            if (c.getWasteOrderStatus()!=1){
                String siteId = c.getSiteId();
                SiteDomain siteDomain = siteMapper.selectById(siteId);
                userWasteOrderDto.setSiteName(siteDomain.getSiteName());
            }else {
                userWasteOrderDto.setSiteName("暂无站点接单");
            }
            userWasteOrderDto.setWasteOrderStatus(c.getWasteOrderStatus());
            return userWasteOrderDto;
        }).collect(Collectors.toList());
        Page<UserWasteOrderDto> userWasteOrderDtoPage = new Page<>();
        Copy.copy(wasteOrderDomainPage,userWasteOrderDtoPage);
        userWasteOrderDtoPage.setRecords(collect1);
        return userWasteOrderDtoPage;
    }

    @Override
    public WasteOrderDto selectById(String id) {
        WasteOrderDomain wasteOrderDomain = mapper.selectById(id);
        System.out.println(wasteOrderDomain);
        if (ObjectUtils.isNotEmpty(wasteOrderDomain)){
            QueryWrapper<WasteOrderDetailsDomain> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("waste_order_id",wasteOrderDomain.getWasteOrderId());
            List<WasteOrderDetailsDomain> wasteOrderDetails = detailsMapper.selectList(objectQueryWrapper);
            List<WasteOrderDetailsDto> collect = wasteOrderDetails.stream().map(wasteOrderDetails1 -> {
                WasteOrderDetailsDto wasteOrderDetailsDto = new WasteOrderDetailsDto();
                BeanUtils.copyProperties(wasteOrderDetails1, wasteOrderDetailsDto);
                return wasteOrderDetailsDto;
            }).collect(Collectors.toList());
            WasteOrderDto wasteOrderDto = new WasteOrderDto();
            BeanUtils.copyProperties(wasteOrderDomain,wasteOrderDto);
            wasteOrderDto.setWasteOrderDetailsDtoList(collect);
            return wasteOrderDto;
        }
        throw new RuntimeException("查询出错");
    }


}
